﻿//定义公共变量
var dataTable, dialog, operType;

//查询统计部分非必填项ComboBox的数据绑定
function initComboBoxUnRequired(el, url) {
    el.combobox({
        panelHeight: 100,
        url: url,
        valueField: 'id',
        textField: 'text',
        editable: false,
        onLoadSuccess: function () {
            el.combobox('select', '0');
        }
    });
}

//ComboBox的数据绑定
function initComboBox(el, url) {
    el.combobox({
        url: url,
        valueField: 'id',
        textField: 'text',
        required: true,
        editable: false
    });
}

function initTableNoPage(url, cols) {
    dataTable = $('#tt').datagrid({
        fit: true,
        striped: true,
        singleSelect: true,
        url: url,
        method: 'get',
        toolbar: '#tb',
        border: false,
        columns: cols
    });
}

//初始化没有工具条的表格
function initTableWithoutTool(url, cols) {
    dataTable = $('#tt').datagrid({
        fit: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        url: url,
        method: 'get',
        border: false,
        columns: cols
    });
}

//初始化表格
function initTable(url, cols) {
    dataTable = $('#dg').datagrid({
        fit: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        url: url,
        method: 'get',
        toolbar: '#tb',
        border: false,
        columns: cols
    });
  //设置分页控件 
    var p = $('#dg').datagrid('getPager'); 
    $(p).pagination({ 
        pageSize: 10,//每页显示的记录条数，默认为10 
        pageList: [5,10,15],//可以设置每页记录条数的列表 
        beforePageText: '第',//页数文本框前显示的汉字 
        afterPageText: '页    共 {pages} 页', 
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录', 
        /*onBeforeRefresh:function(){
            $(this).pagination('loading');
            alert('before refresh');
            $(this).pagination('loaded');
        }*/ 
    });     
}

function init_combobox_tree(combobox_el,url){
	combobox_el.combotree({
	    url:url,
	    valueField:'id',
	    textField:'text',
	    required: true,
        editable: false	    
	});
}

function init_combobox_with_checkboxstyle(el,url){
	el.combobox({
        url:url,
        method:'get',
        valueField:'id',
        textField:'text',
        panelHeight:'auto',
        multiple:true,
        formatter: function (row) {
            var opts = $(this).combobox('options');
            return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
        },
        onLoadSuccess: function () {
            var opts = $(this).combobox('options');
            var target = this;
            var values = $(target).combobox('getValues');
            $.map(values, function (value) {
                var el = opts.finder.getEl(target, value);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            })
        },
        onSelect: function (row) {
            //console.log(row);
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', true);
        },
        onUnselect: function (row) {
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', false);
        }
    });	
}

//初始化对话框
function initDialog(dlg,fn) {
    dialogObj = $('#' + dlg).dialog({
        closed: true,
        cache: false,
        modal: true,
        buttons: [{
            text: '保存',
            iconCls: 'icon-ok',
            handler:fn
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#' + dlg).dialog('close');
            }
        }]
    });
    return dialogObj;
}

/**
 *在点击排序字段时，改变传入后台的字段
 *param对应onBeforeLoad事件的参数
 *map自定义的字段映射Map
 */
onSortColumn=function(param,map){
	//取出map中字段的映射关系值
	var fieldSort=map[param.sort];
	if(fieldSort!='' && fieldSort!=undefined){
		//设置新的排序字段名，设置完之后，发送请求时一并会发送到服务端
		param.sort=fieldSort;
	}
}